//
//  TalkRootViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/2.
//

#import "TalkRootViewController.h"
#import "WebViewJavascriptBridge.h"
#import "BaseRequestApi.h"

@interface TalkRootViewController ()<WKNavigationDelegate>

@property WebViewJavascriptBridge* bridge;

@end

@implementation TalkRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_bridge) { return; }
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:CGRectZero];
    [webView setOpaque:NO];
    webView.navigationDelegate = self;
    webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self loadExamplePage:webView];
    
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.bottom.equalTo(self.view).offset(-60-SafeAreaBottomHeight);
    }];

    [WebViewJavascriptBridge enableLogging];
    _bridge = [WebViewJavascriptBridge bridgeForWebView:webView];
    [_bridge setWebViewDelegate:self];
    
    [_bridge registerHandler:@"loadUserInfo" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"testObjcCallback called: %@", data);
        NSString *token = [USERDEFAULTS valueForKey:@"Token"];
        NSString *language = [[CycLanguage sharedInstance] currentLanguage];
        responseCallback(@{@"token":token,@"lang":language});
    }];
    [_bridge registerHandler:@"getTopHeight" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"testObjcCallback called: %@", data);
        NSString *height = [NSString stringWithFormat:@"%f",SafeAreaTopHeight];
        responseCallback(@{@"height":height});
    }];
    
    [_bridge registerHandler:@"onBackPress" handler:^(id data, WVJBResponseCallback responseCallback) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"webViewDidStartLoad");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"webViewDidFinishLoad");
}

- (void)loadExamplePage:(WKWebView*)webView {
    NSString *token = [USERDEFAULTS valueForKey:@"Token"];
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];

    NSLog(@"**** *- *-* - %@",[NSString stringWithFormat:@"%@%@",Cyc_WEBURL_TALK,token]);
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@&language=%@",Cyc_WEBURL_TALK,token,language]]]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}

@end
